package com.vf.admin.client.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.vf.admin.client.dao.VipOrderDao;
import com.vf.admin.client.service.AppPrivilegeService;
import com.vf.admin.client.service.AppUserService;
import com.vf.admin.client.service.VipOrderService;
import com.vf.admin.client.service.VipPackageService;
import com.vf.admin.client.tx.utils.TxIm;
import com.vf.admin.client.tx.vo.UserShiZhi;
import com.vf.admin.client.tx.vo.userSignVo;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.miaodong.entity.VipOrder;
import com.vf.admin.miaodong.entity.VipPackage;
import com.vf.admin.utils.DatesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.vf.admin.utils.StringEx.isNull;

@Service
public class VipOrderServiceImpl implements VipOrderService {

    @Autowired
    private VipOrderDao vipOrderDao;
    @Autowired
    private AppUserService appUserService;
    @Autowired
    private VipPackageService vipPackageService;
    @Autowired
    AppPrivilegeService appPrivilegeService;

    @Override
    public List<VipOrder> page(Map<String, Object> paraMap, SplitPageBean splitPage) {
        return vipOrderDao.page(paraMap, splitPage);
    }

    @Override
    public List<VipOrder> list() {
        return vipOrderDao.list();
    }

    @Override
    public VipOrder info(Integer id) {
        return vipOrderDao.get(id, VipOrder.class);
    }

    @Override
    public VipOrder infoByOrderNo(String orderNo) {
        return vipOrderDao.infoByOrderNo(orderNo);
    }

    @Override
    public void save(VipOrder params) {
        vipOrderDao.add(params);
    }


    @Override
    public void updateVipWeb(Integer  userId,Integer monthNum) {

            //用户会员到期日更新
            AppUser user = appUserService.get(userId);
            if (user.getVipEndDate() == null) {
                Date date = DatesUtil.addMonth(new Date(), monthNum);
                String s = DatesUtil.dateToStr(date, "yyyy-MM-dd") + " 23:59:59";
                Date date1 = DatesUtil.strToDate(s, "yyyy-MM-dd HH:mm:ss");
                user.setVipEndDate(date1);
            } else {
                Date date = DatesUtil.addMonth(user.getVipEndDate(), monthNum);
                String s = DatesUtil.dateToStr(date, "yyyy-MM-dd") + " 23:59:59";
                Date date1 = DatesUtil.strToDate(s, "yyyy-MM-dd HH:mm:ss");
                user.setVipEndDate(date1);
            }
            appUserService.update(user);
            //升级贵族
            appPrivilegeService.isUpLevel(user);
            //获取用户腾讯方最新的数据
            String re = TxIm.portraitGget(user.getTengxuncode());
            Integer headHalo = 0;
            Integer charmNum = 0;
            Integer status = 0;
            Integer nobleid = 0;
            String headWear = "";
            if (!isNull(re)) {
                JSONObject jsonObject1 = JSON.parseObject(re);
                headHalo = jsonObject1.getInteger("headHalo");
                charmNum = jsonObject1.getInteger("charmNum");
                headWear = jsonObject1.getString("headWear");
                status = jsonObject1.getInteger("status");
                nobleid = jsonObject1.getInteger("nobleid");
            }
            //重新设置资料
            userSignVo vo1 = new userSignVo();
            vo1.setId(user.getId());
            vo1.setCharmNum(charmNum);
            vo1.setHeadWear(headWear);
            vo1.setHeadHalo(headHalo);
            vo1.setNobleid(nobleid);
            vo1.setStatus(status);
            vo1.setIsVip(1);
            net.sf.json.JSONObject obj1 = net.sf.json.JSONObject.fromObject(vo1);
            UserShiZhi shiZhi = new UserShiZhi();
            shiZhi.setUserSign(obj1.toString());
            shiZhi.setTengxuncode(user.getTengxuncode() + "");
            TxIm.portraitSet(shiZhi);
        }

    @Override
    public void refund(VipOrder vipOrder) {
        vipOrder.setRefundStatus(1);
        vipOrder.setRefundTime(new Date());
        vipOrderDao.update(vipOrder);
    }


    @Override
    public void successCall(String orderNo) {
        VipOrder params = infoByOrderNo(orderNo);
        if (params.getStatus() == 0) {
            //test
            //更新订单状态
            params.setStatus(1);
            vipOrderDao.update(params);
            //用户会员到期日更新
            AppUser user = appUserService.get(params.getUserId());
            VipPackage vipPackage = vipPackageService.info(params.getVipPackageId());
            if (user.getVipEndDate() == null) {
                Date date;
                if(7 == vipPackage.getNum()){
                    date = DatesUtil.addDayDate(new Date(), vipPackage.getNum());
                }else {
                    date = DatesUtil.addMonth(new Date(), vipPackage.getNum());
                }
                String s = DatesUtil.dateToStr(date, "yyyy-MM-dd") + " 23:59:59";
                Date date1 = DatesUtil.strToDate(s, "yyyy-MM-dd HH:mm:ss");
                user.setVipEndDate(date1);
            } else {
                Date date;
                if(7 == vipPackage.getNum()){
                    date = DatesUtil.addDayDate(user.getVipEndDate(), vipPackage.getNum());
                }else {
                    date = DatesUtil.addMonth(user.getVipEndDate(), vipPackage.getNum());
                }
                String s = DatesUtil.dateToStr(date, "yyyy-MM-dd") + " 23:59:59";
                Date date1 = DatesUtil.strToDate(s, "yyyy-MM-dd HH:mm:ss");
                user.setVipEndDate(date1);
            }
            //日期计算
            BigDecimal decimal = new BigDecimal(String.valueOf(user.getChongzhimoney())).add(vipPackage.getPrice());
            user.setChongzhimoney(Double.valueOf(decimal.toPlainString()));
            user.setVip(1);
            user.setWindowTicket(1);
            appUserService.update(user);
            //升级贵族
            appPrivilegeService.isUpLevel(user);

            //获取用户腾讯方最新的数据
            String re = TxIm.portraitGget(user.getTengxuncode());
            Integer headHalo = 0;
            Integer charmNum = 0;
            Integer status = 0;
            Integer nobleid = 0;
            String headWear = "";
            if (!isNull(re)) {
                JSONObject jsonObject1 = JSON.parseObject(re);
                headHalo = jsonObject1.getInteger("headHalo");
                charmNum = jsonObject1.getInteger("charmNum");
                headWear = jsonObject1.getString("headWear");
                status = jsonObject1.getInteger("status");
                nobleid = jsonObject1.getInteger("nobleid");
            }
            //重新设置资料
            userSignVo vo1 = new userSignVo();
            vo1.setId(user.getId());
            vo1.setCharmNum(charmNum);
            vo1.setHeadWear(headWear);
            vo1.setHeadHalo(headHalo);
            vo1.setNobleid(nobleid);
            vo1.setStatus(status);
            vo1.setIsVip(1);
            net.sf.json.JSONObject obj1 = net.sf.json.JSONObject.fromObject(vo1);
            UserShiZhi shiZhi = new UserShiZhi();
            shiZhi.setUserSign(obj1.toString());
            shiZhi.setTengxuncode(user.getTengxuncode() + "");
            TxIm.portraitSet(shiZhi);
        }
    }

    @Override
    public VipOrder getByOrderNum(String orderNo, Integer type, Integer status) {
        return vipOrderDao.getByOrderNum(orderNo, type, status);
    }
}
